package com.xszx.dao.impl;

import com.xszx.bean.GoodsType;
import com.xszx.dao.GoodsTypeDao;
import com.xszx.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    @Override
    public void addShopType(GoodsType goodsType) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "insert into t_goodstype values(null, ?, ?, ?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, goodsType.getGoodsType());
            ps.setString(2, goodsType.getMark());
            ps.setInt(3, goodsType.getParentId());

            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public GoodsType getShopTypeById(Integer id) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_goodstype where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                String goodstype = resultSet.getString("goodstype");
                String mark = resultSet.getString("mark");
                Integer parentId = Integer.parseInt(resultSet.getString("parent_id"));

                return new GoodsType(id, goodstype, mark, parentId);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<GoodsType> getAllShopType() {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_goodstype";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet resultSet = ps.executeQuery();

            List<GoodsType> list = new ArrayList<>();
            while (resultSet.next()) {
                Integer id = Integer.parseInt(resultSet.getString("id"));
                String goodstype = resultSet.getString("goodstype");
                String mark = resultSet.getString("mark");
                Integer parentId = Integer.parseInt(resultSet.getString("parent_id"));

                list.add(new GoodsType(id, goodstype, mark, parentId));
            }

            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateShopType(GoodsType goodsType) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_goodstype set goodstype = ?, mark = ? where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, goodsType.getGoodsType());
            ps.setString(2, goodsType.getMark());
            ps.setInt(3, goodsType.getId());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteShopType(List<Integer> ids) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "delete from t_goodstype where id = (?";
            for (int i = 1; i < ids.size(); i++) {
                sql += ", ?";
            }
            sql += ")";
            PreparedStatement ps = connection.prepareStatement(sql);
            for (int i = 0; i < ids.size(); i++) {
                ps.setInt(i+1, ids.get(i));
            }
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
